# -*- coding: utf8 -*-

from project.mysql.table import Table

class TableImage( Table ):

    _table_name = "image"

    def create( self ):
        cur = self.getCursor()

        # Create table
        cur.execute(
            "CREATE TABLE IF NOT EXISTS %s (" % self._table_name+
                "id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY, "+
                "file_id INTEGER UNSIGNED NOT NULL, "+
                "size INTEGER UNSIGNED NOT NULL, "+
                "is_gray TINYINT(1) UNSIGNED NOT NULL, "+
                "width SMALLINT UNSIGNED NOT NULL, "+
                "height SMALLINT UNSIGNED NOT NULL, "+
                "mime VARCHAR(255) NOT NULL "+
            ") ENGINE %s" % self.getEngine()
        )
        try:
            cur.execute(
                "ALTER TABLE `%s` ADD INDEX files (file_id)" % self._table_name
            )
        except:
            pass
        cur.close()
        return


    def insert( self, file_id, mime, size, is_gray, width, height ):

        cur = self.getCursor()

        rows = cur.execute("INSERT INTO image("+
                    "file_id, mime, size, is_gray, width, height"+
                    ") VALUES ("+
                    "%(file_id)s, %(mime)s, %(size)s, %(is_gray)s, %(width)s, %(height)s"+
                    ")", {
            "file_id":file_id,
            "mime":mime,
            "size":size,
            "is_gray":is_gray,
            "width":width,
            "height":height,
        })
        self.getConnection().commit()

        image_id = None
        if rows:
            image_id = self.getConnection().insert_id()

        cur.close()
        return image_id

    def get(self, image_id):
        cur = self.getCursor()
        cur.execute(
            "SELECT * FROM image WHERE id=%(value)s",
            {"value": image_id}
        )
        item = cur.fetchone()
        cur.close()
        if not item:
            item = False
        return item

    def get_count(self):
        cur = self.getCursor()
        cur.execute("SELECT COUNT(1) as c FROM image")
        item = cur.fetchone()
        cur.close()
        if not item:
            item = 0
        return item['c']

    def get_in_file_count(self, file_id):
        cur = self.getCursor()
        cur.execute("SELECT COUNT(1) as c FROM image WHERE file_id=%(id)s", {"id":file_id})
        item = cur.fetchone()
        cur.close()
        if not item:
            item = {'c':0}
        return item['c']

    def remove_file_images(self, file_id):
        cur = self.getCursor()
        rc = cur.execute("DELETE FROM image WHERE file_id=%(id)s", {"id":file_id})
        self.getConnection().commit()
        cur.close()
        return rc

    def get_images(self, limit, offset):
        cur = self.getCursor()
        cur.execute("SELECT * FROM image WHERE id BETWEEN %(o1)s AND %(o2)s ORDER BY id",
                {"o1":offset,"o2":offset+limit-1})
        items = cur.fetchall()
        cur.close()
        return items

    pass
